<template>
  <div class="app-container">
      <el-form :inline="true" :model="listQuery" class="demo-form-inline" @submit.native.prevent>
        <el-form-item label="活动名称">
          <el-input v-model="listQuery.q_name" size="small"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select
            class="toolbar-lable-right"
            v-model="listQuery.q_status"
            placeholder="请选择" size="small">
            <el-option
              v-for="item in actStatus"
              :label="item.name"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="渠道">
          <el-select
            class="toolbar-lable-right"
            v-model="listQuery.q_channel"
            placeholder="请选择" size="small">
            <el-option
              v-for="item in channel"
              :label="item.name"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="small" @click="this.getList">查询</el-button>
          <el-button icon="el-icon-refresh" size="small" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>

      <div class="button-table-margin">
        <el-button size="small" type="primary" @click="add" v-permission="['/admin/promotion/special/create']">添加</el-button>
        <el-button size="small" type="primary" @click="edit" v-permission="['/admin/promotion/special/update']">修改</el-button>
        <el-button size="small" type="primary" @click="del" v-permission="['/admin/promotion/special/delete']">删除</el-button>
        <el-button size="small" type="primary" @click="recruitstart" v-permission="['/admin/promotion/special/specialrecruitstart']">征集商品</el-button>
        <el-button size="small" type="primary" @click="recruitend" v-permission="['/admin/promotion/special/specialrecruitend']">征集结束</el-button>
        <el-button size="small" type="primary" @click="audit" v-permission="['/admin/promotion/special/doaudit']">审核商品</el-button>
        <el-button size="small" type="primary" @click="gridUp" v-permission="['/admin/promotion/special/specialup']">上架</el-button>
        <el-button size="small" type="primary" @click="gridDown" v-permission="['/admin/promotion/special/specialdown']">下架</el-button>
        <el-button size="small" type="primary" @click="abolish" v-permission="['/admin/promotion/special/abolish']">作废</el-button>
        <el-button size="small" type="primary" @click="toProductSort" v-permission="['/admin/promotion/special/sortIsTop']">修改排序</el-button>
        <el-button size="small" type="primary" @click="detail" v-permission="['/admin/promotion/special/detail']">详情</el-button>
      </div>
      <el-table
        ref="singleTable"
        v-loading="listLoading"
        :data="dataList"
        border
        fit
        highlight-current-row
        @current-change="handleCurrentChange"
        style="width: 100%">
        <el-table-column type="index" align="center" width="50"></el-table-column>
        <el-table-column property="name" width="200" label="活动名称"></el-table-column>
        <el-table-column property="typeName" width="200" label="专题分类"></el-table-column>
        <el-table-column property="typeState" width="200" label="分类状态">
          <template slot-scope="scope">
            {{ codeBox.SPECIAL_TYPE_STATE[scope.row.typeState] }}
          </template>
        </el-table-column>
        <el-table-column property="channel" width="200" label="活动应用渠道">
          <template slot-scope="scope">
            {{ codeBox.CHANNEL[scope.row.channel] }}
          </template>
        </el-table-column>
        <el-table-column property="status" width="200" label="使用状态">
          <template slot-scope="scope">
            {{ codeBox.ACT_SPECIAL_STATUS[scope.row.status] }}
          </template>
        </el-table-column>
        <el-table-column property="lowPrice" width="200" label="最低价格"></el-table-column>
        <el-table-column property="lowDiscount" width="200" label="最低折扣"></el-table-column>
        <el-table-column property="startTime" width="200" label="活动开始时间"></el-table-column>
        <el-table-column property="endTime" width="200" label="活动结束时间"></el-table-column>
        <el-table-column property="sortNum" width="200" label="排序"></el-table-column>
        <el-table-column property="isBest" width="200" label="是否推荐">
          <template slot-scope="scope">
            {{ codeBox.YES_NO[scope.row.isBest] }}
          </template>
        </el-table-column>
        <el-table-column property="createUserName" width="200" label="创建人"></el-table-column>
        <el-table-column property="createTime" width="200" label="添加时间"></el-table-column>
        <el-table-column property="updateUserName" width="200" label="最后修改人"></el-table-column>
        <el-table-column property="updateTime" width="200" label="修改更新时间"></el-table-column>
        <el-table-column fixed="right" label="操作" width="100" align="center">
          <template slot-scope="scope">
            <el-button @click="auditRowUp(scope.row)" type="text" size="small">上架</el-button>
            <el-button @click="auditRowDown(scope.row)" type="text" size="small">下架</el-button>
          </template>
        </el-table-column>
      </el-table>
      <Pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </div>

</template>

<script>
import { listActSpecial,deleteActSpecial,recruitstartActSpecial,recruitendActSpecial,
          upActSpecial, downActSpecial, doabolishActSpecial} from '@/api/promotion'
import Pagination from '@/components/Pagination'
import { Message } from 'element-ui'
import { getCodeManager,codeToArray} from '@/api/all'
export default {
  name: 'speciallist',
  components: { Pagination },
  data() {
    return {
      currentRow: null,
      dataList: [],
      actStatus: [],
      channel: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        q_name: '',
        q_channel: '',
        q_status: ''
      },
      dialogFormVisible: false
    }
  },
  created() {
    getCodeManager('ACT_SPECIAL_STATUS,CHANNEL,SPECIAL_TYPE_STATE,YES_NO', () => {
      this.actStatus = codeToArray('ACT_SPECIAL_STATUS');
      this.channel = codeToArray('CHANNEL');
    })
    this.getList()
  },
  methods:{
    handleCurrentChange(val) {
      this.currentRow = val
    },
    // 获取列表数据
    getList() {
      this.listLoading = true
      listActSpecial(this.listQuery).then(response => {
        this.dataList = response.data.rows
        this.total = response.data.total
        this.listLoading = false
      })
    },
    reset() {
      this.listQuery = {
        page: 1,
        limit: 20,
        q_name: '',
        q_channel: '',
        q_status: ''
      }
      this.getList()
    },
    add() {
      this.$router.push({
        path: "/promotion/specialadd"
      });
    },
    edit() {
      if(this.currentRow == null) {
        Message.error('请选择一条记录')
        return
      }
      if(this.currentRow.status != 1) {
        Message.error('只有在新建状态下才能进行修改')
        return
      }
      this.$router.push({
        path: "/promotion/specialedit",
        query: { id: this.currentRow.id }
      });
    },
    del() {
      if(this.currentRow == null) {
        Message.error('请选择一条记录')
        return
      }
      if(this.currentRow.status != 1) {
        Message.error('只能删除新建状态的活动')
        return
      }
      this.$confirm('确定要删除吗？', '提示', {
        onfirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteActSpecial({id: this.currentRow.id}).then(res => {
          if(res.data.success) {
            this.$message({
              type: 'success',
              message: '操作成功!'
            });
            this.getList()
          } else {
            this.$message({
              type: 'error',
              message: res.data.message
            });
          }
        })
      })
    },
    recruitstart() {
      if(this.currentRow == null) {
        Message.error('请选择一条记录')
        return
      }
      if(this.currentRow.status != 1) {
        Message.error('只能对新建状态的活动发起商品征集')
        return
      }
      this.$confirm('确定要开始征集商品吗？', '提示', {
        onfirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        recruitstartActSpecial({id: this.currentRow.id}).then(res => {
          if(res.data.success) {
            this.$message({
              type: 'success',
              message: '操作成功!'
            });
            this.getList()
          } else {
            this.$message({
              type: 'error',
              message: res.data.message
            });
          }
        })
      })
    },
    recruitend() {
      if(this.currentRow == null) {
        Message.error('请选择一条记录')
        return
      }
      if(this.currentRow.status != 2) {
        Message.error('只能对正在征集商品的活动进行停止征集操作')
        return
      }
      this.$confirm('确定要征集结束吗？', '提示', {
        onfirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        recruitendActSpecial({id: this.currentRow.id}).then(res => {
          if(res.data.success) {
            this.$message({
              type: 'success',
              message: '操作成功!'
            });
            this.getList()
          } else {
            this.$message({
              type: 'error',
              message: res.data.message
            });
          }
        })
      })
    },
    audit() {
      if(this.currentRow == null) {
        Message.error('请选择一条记录')
        return
      }
      if(this.currentRow.status != 3) {
        Message.error('只能对已经结束征集商品的活动进行审核操作')
        return
      }
      this.$router.push({
        path: "/promotion/specialaudit",
        query: { id: this.currentRow.id }
      });
    },
    gridUp() {
      if(this.currentRow == null) {
        Message.error('请选择一条记录')
        return
      }
      if(this.currentRow.status == 5) {
        Message.error('已经是上架状态，请勿重复操作')
        return
      }
      if(this.currentRow.status != 3) {
        Message.error('只能上架已经结束征集商品的活动')
        return
      }
      this.$confirm('确定上架吗？', '提示', {
        onfirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        upActSpecial({id: this.currentRow.id}).then(res => {
          if(res.data.success) {
            this.$message({
              type: 'success',
              message: '操作成功!'
            });
            this.getList()
          } else {
            this.$message({
              type: 'error',
              message: res.data.message
            });
          }
        })
      })
    },
    gridDown() {
      if(this.currentRow == null) {
        Message.error('请选择一条记录')
        return
      }
      if(this.currentRow.status == 6) {
        Message.error('经是下架状态，请勿重复操作')
        return
      }
      if(this.currentRow.status != 5) {
        Message.error('只能对上架状态的活动进行下架')
        return
      }
      this.$confirm('确定下架吗？', '提示', {
        onfirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        downActSpecial({id: this.currentRow.id}).then(res => {
          if(res.data.success) {
            this.$message({
              type: 'success',
              message: '操作成功!'
            });
            this.getList()
          } else {
            this.$message({
              type: 'error',
              message: res.data.message
            });
          }
        })
      })
    },
    abolish() {
      if(this.currentRow == null) {
        Message.error('请选择一条记录')
        return
      }
      if(this.currentRow.status != 3) {
        Message.error('只能对已经结束征集商品的活动进行作废操作')
        return
      }
      this.$confirm('确定作废吗，操作是不可逆的？', '提示', {
        onfirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        doabolishActSpecial({id: this.currentRow.id}).then(res => {
          if(res.data.success) {
            this.$message({
              type: 'success',
              message: '操作成功!'
            });
            this.getList()
          } else {
            this.$message({
              type: 'error',
              message: res.data.message
            });
          }
        })
      })
    },
    toProductSort() {
      if(this.currentRow == null) {
        Message.error('请选择一条记录')
        return
      }
      this.$router.push({
        path: "/promotion/specialsort",
        query: { id: this.currentRow.id }
      });
    },
    detail() {
      if(this.currentRow == null) {
        Message.error('请选择一条记录')
        return
      }
      this.$router.push({
        path: "/promotion/specialdetail",
        query: { id: this.currentRow.id }
      });
    },
    auditRowUp(currentRowDo) {
      if(currentRowDo.status == 5) {
        Message.error('已经是上架状态，请勿重复操作')
        return
      }
      if(currentRowDo.status != 3) {
        Message.error('只能上架已经结束征集商品的活动')
        return
      }
      this.$confirm('确定上架吗？', '提示', {
        onfirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        upActSpecial({id: currentRowDo.id}).then(res => {
          if(res.data.success) {
            this.$message({
              type: 'success',
              message: '操作成功!'
            });
            this.getList()
          } else {
            this.$message({
              type: 'error',
              message: res.data.message
            });
          }
        })
      })
    },
    auditRowDown(currentRowDo) {
      if(currentRowDo.status == 6) {
        Message.error('经是下架状态，请勿重复操作')
        return
      }
      if(currentRowDo.status != 5) {
        Message.error('只能对上架状态的活动进行下架')
        return
      }
      this.$confirm('确定下架吗？', '提示', {
        onfirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        downActSpecial({id: currentRowDo.id}).then(res => {
          if(res.data.success) {
            this.$message({
              type: 'success',
              message: '操作成功!'
            });
            this.getList()
          } else {
            this.$message({
              type: 'error',
              message: res.data.message
            });
          }
        })
      })
    }


  }
}
</script>

<style>
</style>
